package com.lg.happyfarmer.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import xtom.frame.util.XtomLogger;

/**
 *
 */
public class BaseDbHelper extends SQLiteOpenHelper {
    private static final String DBNAME = "yjnh.db";
    /**
     * 系统初始化信息
     */
    protected static final String SYSINITINFO = "sysinfo";
    /**
     * 当前登录用户信息
     */
    protected static final String USER = "user";
    /**
     * 访问城市缓存信息
     */
    protected static final String VISIT_CITYS = "visit_citys";
    /**
     * 搜索词缓存
     */
    protected static final String SYS_CASCADE_SEARCH = "sys_cascade_search";
    /**
     * 唠嗑的搜索历史表
     */
    protected static final String SEARCH_LAOKE_HISTORY = "search_laoke_history";
    /**
     * 所有的
     */
    protected static final String ALL_CITYS = "all_citys";

    public BaseDbHelper(Context context) {
        super(context, DBNAME, null, 7);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建系统初始化信息缓存表
        String sys = "sys_web_service text,sys_plugins text,start_img text,android_must_update text,android_last_version text,"
                + "sys_chat_ip text,sys_chat_port text,sys_pagesize text,sys_service_phone text,android_update_url text,"
                + "msg_invite text,welcome_content text";
        String sysSQL = "create table " + SYSINITINFO
                + " (id integer primary key," + sys + ")";
        db.execSQL(sysSQL);

        // 创建当前登录用户信息缓存表
        String user = "id text,username  text,email text,nickname text,mobile text,password text,charindex text,"
                + "sex text,age text,avatar text,avatarbig text,district_name text,lng text,lat text,regdate text,"
                + "score text,role text,authflag text,land_id text,feeaccount text,token text,"
                + "android_must_update text,android_last_version text,android_update_url text,bankuser text,"
                + "bankname text,bankcard text,bankaddress text,alipay text,codeurl text,level_score text,farmer_agent text";

        String userSQL = "create table " + USER + " (" + user + ")";
        db.execSQL(userSQL);

        // 创建搜索词缓存表
        String search = "searchname text";
        String searchSQL = "create table " + SYS_CASCADE_SEARCH + " (" + search
                + ")";
        db.execSQL(searchSQL);

        // 创建唠嗑搜索词缓存表
        String searchLaoke = "searchname text";
        String searchLaokeSQL = "create table " + SEARCH_LAOKE_HISTORY + " (" + searchLaoke
                + ")";
        db.execSQL(searchLaokeSQL);

        // 创建访问城市缓存表
        String citys = "id text,name text,parentid text,nodepath text,namepath text,charindex text,level text,orderby text";
        String citysSQL = "create table " + VISIT_CITYS + " (" + citys + ")";
        db.execSQL(citysSQL);

        // 所有城市缓存表
        String allCity = "id text,name text,parentid text,nodepath text,namepath text,charindex text,level text,orderby text";
        String allCitySQL = "create table " + ALL_CITYS + " (" + allCity + ")";
        db.execSQL(allCitySQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
        if (newV == 3) {
            XtomLogger.w("onUpgrade", "缓存数据库升级-->3");
            String sql = "drop table " + USER;
            db.execSQL(sql);
            // 重新 创建当前登录用户信息缓存表
            String user = "id text,username  text,email text,nickname text,mobile text,password text,charindex text,"
                    + "sex text,age text,avatar text,avatarbig text,district_name text,lng text,lat text,regdate text,"
                    + "score text,role text,authflag text,land_id text,feeaccount text,token text,"
                    + "android_must_update text,android_last_version text,android_update_url text,bankuser text,"
                    + "bankname text,bankcard text,bankaddress text,alipay text,codeurl text,level_score text";
            String userSQL = "create table " + USER + " (" + user + ")";
            db.execSQL(userSQL);
        } else if (newV == 4) {
            XtomLogger.w("onUpgrade", "缓存数据库升级-->4");
            String sql = "drop table " + SYSINITINFO;
            db.execSQL(sql);
            String sys = "sys_web_service text,sys_plugins text,start_img text,android_must_update text,android_last_version text,"
                    + "sys_chat_ip text,sys_chat_port text,sys_pagesize text,sys_service_phone text,android_update_url text,"
                    + "msg_invite text,welcome_content text";
            String userSQL = "create table " + SYSINITINFO + " (id integer primary key," + sys + ")";
            db.execSQL(userSQL);
        } else if (newV == 5) {
            String searchLaoke = "searchname text";
            String searchLaokeSQL = "create table " + SEARCH_LAOKE_HISTORY + " (" + searchLaoke
                    + ")";
            db.execSQL(searchLaokeSQL);
        } else if (newV == 6) {
            // 所有城市缓存表
            String allCity = "id text,name text,parentid text,nodepath text,namepath text,charindex text,level text,orderby text";
            String allCitySQL = "create table " + ALL_CITYS + " (" + allCity + ")";
            db.execSQL(allCitySQL);
        } else if (newV == 7) {
            XtomLogger.w("onUpgrade", "缓存数据库升级-->7");
            String sql = "drop table " + USER;
            db.execSQL(sql);
            // 重新 创建当前登录用户信息缓存表
            String user = "id text,username  text,email text,nickname text,mobile text,password text,charindex text,"
                    + "sex text,age text,avatar text,avatarbig text,district_name text,lng text,lat text,regdate text,"
                    + "score text,role text,authflag text,land_id text,feeaccount text,token text,"
                    + "android_must_update text,android_last_version text,android_update_url text,bankuser text,"
                    + "bankname text,bankcard text,bankaddress text,alipay text,codeurl text,level_score text,farmer_agent text";
            String userSQL = "create table " + USER + " (" + user + ")";
            db.execSQL(userSQL);
        }
    }

}
